package chpt05;

public class LocalClassTest {
	public static final double PI = 3.14;
	private int id = 0;
	public void test() {
		
	}

	public Double getAvgArea(int[] arr, int start, int end) {
		int sum = 0;
		class Validator {
			public void showOutter() {
				// 局部类可以访问外部类的属性
				System.out.println(id);
				System.out.println(PI);
				// 局部类不能访问非effectively final的局部变量
				// System.out.println(sum); // 此句编译不通过
			}

			public boolean validate() {
				// 局部类可以访问final或effectively final的局部变量
				if (arr == null || arr.length == 0)
					return false;
				if (start > end)
					return false;
				return true;
			}
		}
		Validator v = new Validator();
		v.showOutter();
		boolean isLeagal = v.validate();
		if (!isLeagal)
			return null;
		for (int i = start; i <= end; i++) {
			sum += arr[i];
		}
		double avg = (double) sum / (end - start + 1);
		// 局部类可以访问外部类的静态属性PI
		return Double.valueOf(PI * avg * avg);
	}

	public static void main(String[] args) {
		int[] radius = { 1, 2, 3, 4, 5 };
		LocalClassTest obj = new LocalClassTest();
		Double avgArea = obj.getAvgArea(radius, 3, 1);
		System.out.println(avgArea);
		avgArea = obj.getAvgArea(radius, 0, 4);
		System.out.println(avgArea);
	}
}
